const form = document.querySelector("form");
const buttons = form.querySelectorAll("button");

buttons[0].addEventListener("click", () => {
    fetch("get?string=GET-测试用&list=足球,篮球,乒乓球&byteNum=22&bigDecimal=2489.42&user.name=小明", {
        method: "get",
        //是否携带凭据，默认为同源携带(same-origin)
        credentials: "include"
        //请求模式
        // mode: "cors"
        //缓存模式
        // cache: "only-if-cached"
    })
        .then(response => {
            console.log(response);
            if (response.status === 200) {
                return response.json();
            }
        })
        .then(data => {
            console.log('Success:', JSON.stringify(data))
            document.querySelector("#show").innerText = JSON.stringify(data);
        })
        .catch(error => console.error('Error:', error));
});

buttons[1].addEventListener("click", () => {
    const formData = new FormData();
    const files = form.querySelector("input[type='file']").files;
    formData.append("file", files[0]);
    formData.append("string", form.querySelector("input[name='string']").value);

    //多文件上传
    // const files = form.querySelector("input[type='file'][multiple]").files;
    // // formData 只接受文件、Blob 或字符串，不能直接传递数组，所以必须循环嵌入
    // for (let i = 0; i < files.length; i++) {
    //     formData.append('file', files[i]);
    // }

    fetch('test', {
        method: 'post',
        body: formData
    })
        .then(response => response.json())
        .then(data => {
            console.log('Success:', JSON.stringify(data))
            document.querySelector("#show").innerText = JSON.stringify(data);
        })
        .catch(error => console.error('Error:', error));
});